// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:async';

import 'package:flutter/material.dart';
// import 'package:flutter/rendering.dart';
import 'package:junyeyishu/ui/home/widgets/QGTypeSwitchWidget.dart';
import 'package:junyeyishu/ui/sale/widgets/QGBatchOnshelfAlert.dart';

import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/widgets/QGListState.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'widget/QGMineNFTCell.dart';
import 'widget/QGMineNFTListWidget.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGNavigationBar.dart';

class QGMineNFTPage extends StatefulWidget {
  const QGMineNFTPage({super.key});
  @override
  State<QGMineNFTPage> createState() => _QGMineNFTPageState();
}

class _QGMineNFTPageState extends QGListState<QGMineNFTPage> {
  int type = 0;
  var items = ["衍生品", "盲盒"];

  bool hasBatch = false;
  bool batchMode = false;

  StreamSubscription? subscription;
  @override
  void dispose() {
    subscription?.cancel();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    subscription = eventBus.on<EventRefreshMyNFT>().listen((obj) {
      onRefresh();
    });

    listURL = url_user_nftList;
    listKey = "list";

    gridDelegate = SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
      mainAxisSpacing: 15.rpx,
      crossAxisSpacing: 15.rpx,
      childAspectRatio: 165 / 230,
    );

    //权限检测
    Network.post(url_market_haveBatchShelf).then((res) {
      if (!res.success) return;
      hasBatch = res.data["is_have"] == 1;
      if (mounted) setState(() {});
    });

      onRefresh();


  }

  @override
  Map listParam() {
    switch (type) {
      case 0:
        return {"tab_type": 1};
      case 1:
        return {"tab_type": 3};
      case 2:
        return {"tab_type": 2};
      default:
        break;
    }
    return {};
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // backgroundColor: Color(0xFFF8F8F6),
        body:  Column(
          children: [
            buildHeader(),
            buildList(),
          ],
        )
        
       );
  }

  buildHeader() {
    return Container(
      color: Color(0xFF1d3f41),
      child: Column(children: [
        QGNavigationBar(
          title: "我的藏品",
          rightItem: hasBatch
              ? GestureDetector(
                  onTap: () {
                    batchMode = !batchMode;
                    setState(() {});
                  },
                  child: Container(
                    padding: EdgeInsets.all(5.rpx),
                    color: Colors.transparent,
                    child: Text(
                      '批量操作',
                      style: TextStyle(
                          color:
                              batchMode ? Color(0xFF5E85E3) : Color(0xFF111111),
                          fontSize: 13.rpx,
                          fontWeight:
                              batchMode ? FontWeight.bold : FontWeight.normal),
                    ),
                  ),
                )
              : Container(),
        ),
        inReview()
            ? Container()
            : Container(
              alignment: Alignment.center,
              width: 375.rpx,
              color: Color(0xFF0C1012),
              child:SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: QGTypeSwitchWidget(
                  current: type,
                  items: items,
                  underline: true,
                  paddingLen: 25,
                  onSwitch: onSwitch,
                ),
              ),
            )
            ,
      ]),
    );
  }

  buildSearchBar() {
    return Container(
      margin: EdgeInsets.only(left: 10.rpx, right: 10.rpx),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(6.rpx),
        child: Container(
          height: 32.rpx,
          color: Color(0xffF6F6F6),
          child: Row(children: [
            SizedBox(width: 10.rpx),
            QGImage(asset: "sale_search"),
            SizedBox(width: 6.rpx),
            Text(
              '搜索藏品',
              style: TextStyle(
                  color: Color(0xff666666),
                  fontSize: 12.rpx,
                  fontWeight: FontWeight.normal),
            ),
          ]),
        ),
      ),
    );
  }

  onSwitch(index) {
    type = index;
    setState(() {});

    list = null;
    onRefresh();
  }

  @override
  Widget itemBuilder(context, index, data) {
    data?["is_liked"] = int.parse("${data?["is_liked"]}");
    data?["show_like"] = type != 1;

    return GestureDetector(
        onTap: () {
          if (batchMode) {
            onSelect(data);
          } else {
            QGMineNFTListWidget.show(context, data);
          }
        },
        child: QGMineNFTCell(data: data));
  }

  onSelect(data) {
    var ctx = context;
    QGBatchOnshelfAlert.show(context, (price, count, password, type) {
      var param = {};
      if (type) {
        param["pid"] = data["pid"];
        param["price"] = price;
        param["num"] = count;
        param["pay_passwd"] = password;
      } else {
        param["pid"] = data["pid"];
        param["num"] = count;
      }

      var url = type ? url_market_onShelfBatch : url_market_offShelfBatch;
      Map<String, dynamic> stringMap = param.cast<String, dynamic>();

      Network.post(url, data: stringMap, context: ctx).then((res) {
        toast(res.msg);
        if (!res.success) return;
        Navigator.of(context).pop();
        onRefresh();
      });
    });
  }
}
